/**
 * 
 */
package de.damontools.AWoLoWA;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.net.wifi.WifiManager;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

/**
 * @author damon
 * 
 */
public class WiFiCheckService extends Service {
	/*test*/
	private static final String TAG = "WiFiWoL";
	MediaPlayer player;

	TextView textStatus;
	String ssid;
	WifiManager wifi;
	final int INTERVAL = 10000;
	private Timer timer = new Timer();
	boolean connected = false;
	final String ip = "255.255.255.255";
	private String strMAC = "";
	private String strSSID = "";
	final static int PORT = 9;
	final int packagesToSend = 3;
	int sentPackages = 0;
	boolean sendPackages = true;
	private SharedPreferences sharedPreferences;
	private SharedPreferences.Editor editor;
	

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@SuppressWarnings("unused")
	@Override
	public void onCreate() {
		// Toast.makeText(this, "AWoLoWA Service Created",
		// Toast.LENGTH_LONG).show();
		// Log.d(TAG, "onCreate");
		sharedPreferences =  PreferenceManager.getDefaultSharedPreferences(this);
		editor = sharedPreferences.edit();
    	strMAC = WiFiCheck.getMAC();
		strSSID = WiFiCheck.getSSID();
//		int ssidID = sharedPreferences.getInt("SSID", 1);
//		strSSID = 
//		strMAC = sharedPreferences.getString("MAC", "00-00-00-00-00-00");
	}

	@Override
	public void onDestroy() {
		// Log.d(TAG, "onDestroy");
		Toast.makeText(this, "AWoLoWA Service Stopped", Toast.LENGTH_LONG).show();
		stopservice();
	}

	@Override
	public void onStart(Intent intent, int startid) {
		// Log.d(TAG, "onStart");
		Toast.makeText(this, "AWoLoWA Service Started", Toast.LENGTH_LONG).show();
		startservice();
	}

	private void startservice() {

		Thread background = new Thread (new Runnable() {
		    @Override
		    public void run() {
		        // this will be done in the Pipeline Thread
		        /* Do your GPS task here */
		        // active the update handler
		    	timer.scheduleAtFixedRate(new TimerTask() {
		    		public void run() {
		    			try {
		    				checkIT();
		    			} catch (IOException e) {
		    				// TODO Auto-generated catch block
		    				e.printStackTrace();
		    			}
		    			
		    		}
		    	}, 0, INTERVAL);
		    }
		});
		background.start();
	}

	public void checkIT() throws IOException {
		if (checkSSID()) {
			Log.d(TAG, "sending wol-package to: " + ip + "   --   " + strMAC);
			WakeOnLan.wakeup(strMAC);
		}
	}

	public boolean checkSSID() {
		// Setup WiFi
		// Log.d(TAG, "Checking SSID.." );
		wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);

		if (wifi.isWifiEnabled()) {
			// Get WiFi status
			ssid = wifi.getConnectionInfo().getSSID();
			if (ssid != null) {
				if (ssid.equals(strSSID)) {
					if (sendPackages == true) {
						if (sentPackages < packagesToSend) {
							Log.d(TAG, "connected to: " + strSSID);
							sentPackages++;
							return true;
						} else{
							sendPackages = false;
							sentPackages = 0;
							return false;
						}
					} else
						return false;
				} else{
					sendPackages = true;
					return false;
				}
			} else
				return false;
		} else
			return false;

	}

	private void stopservice() {
		if (timer != null) {
			timer.cancel();
		}
	}
}
